package com.spring.ocrm.web.controller;


import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.spring.ocrm.web.model.ReturnOrder;
import com.spring.ocrm.web.service.OCRMService;
import com.spring.security.core.annotation.OaRestPermission;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/ocrm")
public class OCRMController {
	@Resource 
	private OCRMService ocrmService;
	
	
	@OaRestPermission(value="/ocrm/stockin")
	@RequestMapping(value = "/stockin", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "入库接口", httpMethod = "POST", response = Object.class, notes = "入库接口",produces = "application/json; charset=utf-8")    
    public Object stockin(
    		@ApiParam(required = false, name = "productStockinID", value = "入库id")@RequestParam(value="productStockinID",required = false)String productStockinID,
    		HttpServletRequest request
    		) throws Exception{
		BaseResult rt = null;
    	try{
    		rt =ocrmService.stockin(productStockinID, request);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }
	
	@OaRestPermission(value="/ocrm/stockout")
	@RequestMapping(value = "/stockout", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "出库接口", httpMethod = "POST", response = Object.class, notes = "入库接口",produces = "application/json; charset=utf-8")    
    public Object stockout(
    		@ApiParam(required = false, name = "productStockoutID", value = "出库")@RequestParam(value="productStockoutID",required = false)String productStockoutID,
    		HttpServletRequest request
    		) throws Exception{
		BaseResult rt = null;
    	try{
    		rt =ocrmService.stockout(productStockoutID, request);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }

		@OaRestPermission(value="/ocrm/allocate")
		@RequestMapping(value = "/allocate", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "调拨接口", httpMethod = "POST", response = Object.class, notes = "入库接口",produces = "application/json; charset=utf-8")    
	    public Object allocate(
	    		@ApiParam(required = false, name = "crmStorehouseAllocateID", value = "调拨id")@RequestParam(value="crmStorehouseAllocateID",required = false)String crmStorehouseAllocateID,
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt = null;
	    	try{
	    		rt =ocrmService.allocate(crmStorehouseAllocateID, request);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@OaRestPermission(value="/ocrm/returnOrder")
		@RequestMapping(value = "/returnOrder", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "订单退货", httpMethod = "POST", response = Object.class, notes = "入库接口",produces = "application/json; charset=utf-8")    
	    public Object returnOrder(
	    		@RequestBody ReturnOrder ro,
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt = null;
	    	try{
	    		rt =ocrmService.returnOrder(ro, request);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@OaRestPermission(value="/ocrm/returnOrder")
		@RequestMapping(value = "/returnOrderAndExecute", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "订单退货并执行", httpMethod = "POST", response = Object.class, notes = "入库接口",produces = "application/json; charset=utf-8")    
	    public Object returnOrderAndExecute(
	    		@RequestBody ReturnOrder ro,
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt = null;
	    	try{
	    		rt =ocrmService.returnOrder(ro, request);
	    		
	    		if(rt.getStatus().equals("0")){
	    			return rt;
	    		}
	    		HashMap<String,Object> dataset=(HashMap<String,Object>)rt.getDataset();
	    		String crmReturnOrderID=MyTools.ObjectToString(dataset.get("crmReturnOrderID"));
	    		
	    		rt=ocrmService.returnOrderExecute(crmReturnOrderID,request);
	    		
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		

		@OaRestPermission(value="/ocrm/returnOrderExecute")
		@RequestMapping(value = "/returnOrderExecute", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "订单退货执行生效", httpMethod = "POST", response = Object.class, notes = "入库接口",produces = "application/json; charset=utf-8")    
	    public Object returnOrderExecute(
	    		@ApiParam(required = false, name = "crmReturnOrderID", value = "退货id")@RequestParam(value="crmReturnOrderID",required = false)String crmReturnOrderID,
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt = null;
	    	try{
	    		rt =ocrmService.returnOrderExecute(crmReturnOrderID, request);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		//@OaRestPermission(value="/ocrm/returnOrderExecute")
		@RequestMapping(value = "/returnRechargeCardExecute", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "充值卡退货执行生效", httpMethod = "POST", response = Object.class, notes = "入库接口",produces = "application/json; charset=utf-8")    
	    public Object returnRechargeCardExecute(
	    		@ApiParam(required = false, name = "contractID", value = "合同id")@RequestParam(value="contractID",required = false)String contractID,
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt = null;
	    	try{
	    		rt =ocrmService.returnRechargeCardExecute(contractID,request);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@RequestMapping(value = "/returnMemberCardExecute", method = RequestMethod.POST)
	    @ResponseBody
	    @ApiOperation(value = "零售套餐退货执行生效", httpMethod = "POST", response = Object.class, notes = "零售套餐退货执行生效",produces = "application/json; charset=utf-8")    
	    public Object returnMemberCardExecute(
	    		@ApiParam(required = false, name = "contractID", value = "合同id")@RequestParam(value="contractID",required = false)String contractID,
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt = null;
	    	try{
	    		rt =ocrmService.returnMemberCardExecute(contractID, request);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
		
		@RequestMapping(value = "/orderDetail", method = RequestMethod.GET)
	    @ResponseBody
	    @ApiOperation(value = "订单详细", httpMethod = "GET", response = Object.class, notes = "订单详细",produces = "application/json; charset=utf-8")    
	    public Object orderDetail(
	    		@ApiParam(required = false, name = "orderID", value = "会员申请id")@RequestParam(value="orderID",required = false)String orderID,
	    		HttpServletRequest request
	    		) throws Exception{
			BaseResult rt = null;
	    	try{
	    		rt =ocrmService.orderDetail(orderID);
	    	}catch(Exception e){
	    		rt=new BaseResult("0","系统异常");
	    		e.printStackTrace();
	    	}
	        return rt;
	    }
}
